package logic.weapons.projectileWeapons.heatSeeker;

import networking.Synchronizable;
import networking.messages.createMessages.HeatSeekerCreateMessage;
import logic.ships.Ship;
import logic.ships.hunter.Hunter;
import logic.weapons.projectileWeapons.Projectile;

import com.captiveimagination.jgn.synchronization.message.SynchronizeCreateMessage;
import com.jme.math.Vector3f;
import com.jme.scene.Node;
import com.jme.scene.Spatial;

public class HeatSeeker extends Projectile implements Synchronizable {
	
	private static final long serialVersionUID = 1L;
	
	private Spatial target;
	
	public HeatSeeker(Ship ship, Node model, Spatial target, Vector3f startLoc, 
			Vector3f direction, HeatSeekerProperties props, HeatSeekerWeapon weapon, boolean dummy) {
		super(ship, model, startLoc, direction, props, weapon, dummy);
		this.target = target;
		
		if(!dummy) {
			fireController = new HeatSeekerController(this, ship, direction);
			addController(fireController);
		}
		
		if(target != null && target instanceof Hunter) ((Hunter)target).addAttackingHeatSeeker(this);
	}
	
	public Spatial getTarget() { return target; }
	
	public void changeTarget(Spatial newTarget) {
		if(target instanceof Hunter) ((Hunter)target).removeHeatSeeker(this);
		
		target = newTarget;
		((HeatSeekerController)fireController).changeTarget(newTarget);
		
		if(target != null && target instanceof Hunter) ((Hunter)target).addAttackingHeatSeeker(this);
	}
	
	@Override
	public void destroy() {
		if(target instanceof Hunter) ((Hunter)target).removeHeatSeeker(this);
		super.destroy();
	}
	
	public float getAgility() { return ((HeatSeekerProperties)props).getAgility(); }

	@Override
	public SynchronizeCreateMessage getCreateMessage() {
		int weaponID = weapon.getID();
		HeatSeekerCreateMessage hscm =  new HeatSeekerCreateMessage(weaponID, weaponFireID);
		if(weapon.getClientWeaponFireID() != 0) hscm.setupClient(weapon.getClientWeaponFireID());
		return hscm;
	}
}